package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.User;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.system.UserService;
import com.itheima.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.UUID;

public class UserServicreImpl implements UserService {
    @Override
    public PageInfo<User> findAll(String pageStr, String sizeStr) {
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(sizeStr)) {
            size = Integer.parseInt(sizeStr);
        }

        PageHelper.startPage(page, size);
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        List<User> list = dao.findAll();
        System.out.println(list);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void save(User user) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        String ID = UUID.randomUUID().toString();
        //密码必须经过加密处理MD5加密
        user.setPassword(MD5Util.md5(user.getPassword()));
        user.setId(ID);
        dao.save(user);
    }

    @Override
    public User findById(String id) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        return dao.findById(id);
    }

    @Override
    public void update(User user) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        dao.update(user);
    }

    @Override
    public void delete(String id) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        dao.delete(id);
    }


}
